import { createRouter ,createWebHistory} from "vue-router";
import login from '@/components/Login.vue'
import Students from "@/components/Students.vue";
import activation from '@/components/Activation.vue';
import coach from  '@/components/Coach.vue';
import quit from '@/components/Quit.vue';

const router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:'',
            redirect:'/Login'
        },
        //路由规则
        {
            path:'/Login',
            name:'Login',
            component:login
        },
        {
            path:'/Students',
            component:Students,
            name:'Students',
            meta:
            {
                    requireAuth: true,
                    keepAlive: true
            }
        },
        {
            path:'/Activation',
            component:activation,
            name:'Activation',
            meta: {
                requireAuth: true,
                keepAlive: true
            }
        },
        {
            path:'/Coach',
            component:coach,
            name:'Coach',
            meta: {
                requireAuth: true,
                keepAlive: true
            }
        },
        {
            path:'/Quit',
            component:quit,
            name:'Quit',
            meta: {
                requireAuth: true,
                keepAlive: true
            }
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth)
    {
        const token = localStorage.getItem('token');
        if (token)
        {
            next();
        }
        else
        {
            next({
                path: '/Login',
                query: { redirect: to.fullPath }
            })
        }
    }
    else
    {
        next();
    }
})

export default router